﻿Public Class Registration

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Call Common.LaunchToFormition()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Cursor = Cursors.WaitCursor


        Common.BackgroundDataLoadingIgnoreEvents = True

        If Len(TextBox1.Text) <> 36 Then
            Call MsgBox("The registration ID is not long enough. It needs to be 36 characters (include the '-' characters).")
            Common.BackgroundDataLoadingIgnoreEvents = False
            Exit Sub
        End If

        Dim z As New Formition.FormitionPasswordSafe
        If Not TestMode Then
            z.Url = "http://www.formition.com/desktopmodules/formition/formitionpasswordsafe.asmx"
            z.Credentials = System.Net.CredentialCache.DefaultCredentials
            z.Proxy = System.Net.WebProxy.GetDefaultProxy
            z.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
            System.Net.ServicePointManager.Expect100Continue = False
        End If

        Dim SecurityToken As String = Common.Encrypt(Common.SystemKey, "Formition Password Safe")

        Try
            Dim results As String = z.ValidateRegistrationNo(SecurityToken, TextBox1.Text, "")
            If results = "false" Then
                Call MsgBox("The registration code you entered is not valid, please check the number.", MsgBoxStyle.Exclamation, "Formition Password Safe Registration.")

            Else

                'make sure the user is going to be able to validate this registration

                Call Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Formition", "RegistrationID", TextBox1.Text)
                Call Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Formition", "ValidationID", results)

                If Common.ValidateRegistration() Then
                    Passwords.Visible = True
                    Passwords.Enabled = True
                    Me.Cursor = Cursors.Default
                    Call Passwords.SetupScreen("Login")
                    Passwords.Message("Registered", "You have successfully registered your version of Password Safe, thanks for your support!", "Info")

                    Me.Close()
                Else

                    Me.Cursor = Cursors.Default

                    Call MsgBox("Something unexpected has occurred, the website has validated the Registration ID however this program is unable to validate Registration key, please contact support@formition.com for further assistance.", MsgBoxStyle.Critical, "Formitoon Password Registration")
                End If

            End If
        Catch ex As Exception

            Me.Cursor = Cursors.Default

            Call MsgBox("Unable to contact website to validate Registration ID. " & ex.Message, MsgBoxStyle.Exclamation, "Formition Password Safe")


            Exit Sub
        End Try

        Common.BackgroundDataLoadingIgnoreEvents = False
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub Registration_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Common.BackgroundDataLoadingIgnoreEvents = True Then
            Common.BackgroundDataLoadingIgnoreEvents = False
            Exit Sub
        End If

        'if they want to close the rego form, they will also close the program.
        Common.BackgroundDataLoadingIgnoreEvents = True
        If MsgBox("Are you sure you want to close this program?", MsgBoxStyle.OkCancel, "Formition Password Safe") = 1 Then
            e.Cancel = False
            Passwords.Close()
            Application.Exit()
        Else
            e.Cancel = True
        End If


    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Call Common.LaunchToFormition()
    End Sub

    Private Sub cmdRegisterLater_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegisterLater.Click
        Common.RegisterLater = True
        Common.BackgroundDataLoadingIgnoreEvents = True
        Passwords.Visible = True
        Passwords.Enabled = True
        Passwords.WindowState = FormWindowState.Normal
        Me.Cursor = Cursors.Default
        Call Passwords.SetupScreen("Login")
        Common.BackgroundDataLoadingIgnoreEvents = False
        Me.Visible = False
    End Sub
End Class